package com.base;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBPool {

	public static Connection getConnection(String poolName) {
		Connection conn = null;
		try {
//			conn = DBCPoolingListener.getConnection(poolName);
			conn = DBPoolUnContext.getConnection(poolName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static void putConnection(String poolName, java.sql.Connection conn) {
		try {
			DBPoolUnContext.putConnection(poolName, conn);
//			conn.close();
//			conn = null;
		} catch (Exception e) {
		}
	}

	// Remove and close all connections in pool
	public static void release(String poolName) {
	}

	public static int size(String poolName) {
		return 0;
	}

	public static boolean isEmpty(String poolName) {
		return true;
	}

	public static boolean isClosed(String poolName) {
		return false;
	}

	// --------------------------------------------------------------------------
	public static Connection makeDBConnection(String poolName)
			throws SQLException {
		return getConnection(poolName);
	}

	public static void releaseConnection(String poolName, Connection conn,
			PreparedStatement preStmt) {
		// try {if (conn != null) {conn.close(); } } catch (SQLException e) { }
		putConnection(poolName, conn);
		try {
			if (preStmt != null) {
				preStmt.close();
			}
		} catch (SQLException e) {
		}
		
	}

	public static void releaseConnection(String poolName, Connection conn,
			PreparedStatement preStmt, ResultSet rs) {
		releaseConnection(poolName, conn, preStmt);
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
		}
	}

	public static void releaseConnection(String poolName, Connection conn,
			PreparedStatement preStmt, Statement stmt, ResultSet rs) {
		releaseConnection(poolName, conn, preStmt, rs);
		try {
			if (stmt != null) {
				stmt.close();
			}
		} catch (SQLException e) {
		}
	}

	public static void load() {
	}

	public static void main(String args[]) {
		Connection conn = DBPool.getConnection("vtv_media");
		System.out.println(conn);
		DBPool.putConnection("vtv_media", conn);
		DBPool.release("vtv_media");
	}
}
